
use "$out/co_workers_with_controls_event_level_20250704_BM.dta", clear

cap drop first_event
egen first_event=min( pre_month), by(anon)
label var first_event "first event of a person considering both treated and control"
cap drop first_within_treated
*egen first_within_treated=min(pre_month) if treated==1, by(anon)
egen first_within_treated=max(pre_month) if treated==1, by(anon feor1_h2 size_cat)
label var first_within_treated "first treated event of a person"

xtset anon pre_month
drop ACC_*
*codebook anon if treated==1
keep if treated==0 | first_within_treated== pre_month
drop first_event first_within_treated
egen n=seq()
rename vallazon1 matched_vallazon
rename feor1_h2 matched_feor1_h2
tempfile tmp
save `tmp'

* do this by year

* 12/1) first year starting with accident month

use `tmp', clear
expand 12
egen t=seq(), by(n)
tab t
replace t=t+pre_month

* add quarterly values of wage and firm FE

merge m:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 w1 wh1 transfer_mtp transfer_ny nap jnap fogvisz1 )
rename vallazon1 vallazon
merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", keep(master match) nogen keepusing(vall_fix)
rename vall_fix FEj
merge m:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match) nogen
rename passziv_tip1 SLdays

gen EMP=1 if (vallazon!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (SLdays<6 | SLdays==.) & (fogvisz1==201)

* create a winsorized version of the wage variable, only for the EMP=1
gen w1_wins=w1
egen wp1=pctile(w1) if EMP==1, by(t) p(1)
egen wp99=pctile(w1) if EMP==1, by(t) p(99)
replace w1_wins=wp1 if w1_wins<wp1 & w1_wins!=0 & EMP==1
replace w1_wins=wp99 if w1_wins>wp99 & w1_wins!=. & EMP==1
drop wp1 wp99

cap gen ev=2003+int((t-1)/12)
replace ev=2003+int((t-1)/12)

gen defl=1 if ev==2003
	replace defl = 1.067991144 if ev==2004
	replace defl = 1.106443719 if ev==2005
	replace defl = 1.149586344 if ev==2006
	replace defl = 1.241552086 if ev==2007
	replace defl = 1.317292007 if ev==2008
	replace defl = 1.372611279 if ev==2009
	replace defl = 1.439874155 if ev==2010
	replace defl = 1.496329527 if ev==2011
	replace defl = 1.581158238 if ev==2012
	replace defl = 1.608512002 if ev==2013
	replace defl = 1.604987182 if ev==2014
	replace defl = 1.603851084 if ev==2015
	replace defl = 1.610259846 if ev==2016
	replace defl = 1.648100676 if ev==2017

* cenzoring hours worked
replace wh1=40 if wh1>40 & wh1!=.
replace wh1=20 if wh1<20	
foreach X in "" _wins{
	gen lnw1`X'_restr=log((w1`X'/defl)/((wh1*30)/7)) if transfer_mtp!=401 & transfer_ny!=203
	replace lnw1`X'_restr=. if EMP!=1

}
drop EMP

* create the quarterly averages

foreach X in "" _wins{
	foreach Y of numlist 1/4{
		egen mean_q`Y'_w`X'_=mean(lnw1`X'_restr) if t>=pre_month+(3*(`Y'-1)+1) & t<=pre_month+(3*(`Y'-1)+3), by(n)
		egen mean_q`Y'_w`X'=min(mean_q`Y'_w`X'_), by(n)
		drop mean_q`Y'_w`X'_
	}
}
foreach X in ""{
	foreach Y of numlist 1/4{
		egen mean_q`Y'_FEj`X'_=mean(FEj`X') if t>=pre_month+(3*(`Y'-1)+1) & t<=pre_month+(3*(`Y'-1)+3), by(n)
		egen mean_q`Y'_FEj`X'=min(mean_q`Y'_FEj`X'_), by(n)
		drop mean_q`Y'_FEj`X'_
		gen flag_missingFEj`X'_q`Y' = 1 if mean_q`Y'_FEj`X' == .
		recode flag_missingFEj`X'_q`Y' (.=0)
		gen mean_q`Y'_FEj`X'_imp = mean_q`Y'_FEj`X'
		sum mean_q`Y'_FEj`X'
		replace mean_q`Y'_FEj`X'_imp = `r(mean)' if flag_missingFEj`X'_q`Y' == 1
		drop mean_q`Y'_FEj`X'
	}	
}

drop w1 w1_wins wh1 transfer_ny transfer_mtp lnw1_restr lnw1_wins_restr vallazon nap jnap fogvisz1 SLdays FEj ev defl

keep if t==pre_month+1
drop t 

merge 1:1 anon pre_month treated matched_vallazon matched_feor1_h2 size_cat controls_we_used n using `tmp'
assert _merge==3
drop _merge
save `tmp', replace

* 12/2) second year after the accident

use `tmp', clear
drop mean* flag*
expand 12
egen t=seq(), by(n)
tab t
replace t=t+pre_month+12

* add quarterly values of wage and firm FE

merge m:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 w1 wh1 transfer_mtp transfer_ny nap jnap fogvisz1 )
rename vallazon1 vallazon
merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", keep(master match) nogen keepusing(vall_fix)
rename vall_fix FEj
merge m:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match) nogen
rename passziv_tip1 SLdays

gen EMP=1 if (vallazon!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (SLdays<6 | SLdays==.) & (fogvisz1==201)

* create a winsorized version of the wage variable, only for the EMP=1
gen w1_wins=w1
egen wp1=pctile(w1) if EMP==1, by(t) p(1)
egen wp99=pctile(w1) if EMP==1, by(t) p(99)
replace w1_wins=wp1 if w1_wins<wp1 & w1_wins!=0 & EMP==1
replace w1_wins=wp99 if w1_wins>wp99 & w1_wins!=. & EMP==1
drop wp1 wp99

cap gen ev=2003+int((t-1)/12)
replace ev=2003+int((t-1)/12)

gen defl=1 if ev==2003
	replace defl = 1.067991144 if ev==2004
	replace defl = 1.106443719 if ev==2005
	replace defl = 1.149586344 if ev==2006
	replace defl = 1.241552086 if ev==2007
	replace defl = 1.317292007 if ev==2008
	replace defl = 1.372611279 if ev==2009
	replace defl = 1.439874155 if ev==2010
	replace defl = 1.496329527 if ev==2011
	replace defl = 1.581158238 if ev==2012
	replace defl = 1.608512002 if ev==2013
	replace defl = 1.604987182 if ev==2014
	replace defl = 1.603851084 if ev==2015
	replace defl = 1.610259846 if ev==2016
	replace defl = 1.648100676 if ev==2017

* cenzoring hours worked
replace wh1=40 if wh1>40 & wh1!=.
replace wh1=20 if wh1<20	
foreach X in "" _wins{
	gen lnw1`X'_restr=log((w1`X'/defl)/((wh1*30)/7)) if transfer_mtp!=401 & transfer_ny!=203
	replace lnw1`X'_restr=. if EMP!=1

}
drop EMP

* create the quarterly averages

foreach X in "" _wins{
	foreach Y of numlist 5/8{
		egen mean_q`Y'_w`X'_=mean(lnw1`X'_restr) if t>=pre_month+(3*(`Y'-1)+1) & t<=pre_month+(3*(`Y'-1)+3), by(n)
		egen mean_q`Y'_w`X'=min(mean_q`Y'_w`X'_), by(n)
		drop mean_q`Y'_w`X'_
	}
}
foreach X in ""{
	foreach Y of numlist 5/8{
		egen mean_q`Y'_FEj`X'_=mean(FEj`X') if t>=pre_month+(3*(`Y'-1)+1) & t<=pre_month+(3*(`Y'-1)+3), by(n)
		egen mean_q`Y'_FEj`X'=min(mean_q`Y'_FEj`X'_), by(n)
		drop mean_q`Y'_FEj`X'_
		gen flag_missingFEj`X'_q`Y' = 1 if mean_q`Y'_FEj`X' == .
		recode flag_missingFEj`X'_q`Y' (.=0)
		gen mean_q`Y'_FEj`X'_imp = mean_q`Y'_FEj`X'
		sum mean_q`Y'_FEj`X'
		replace mean_q`Y'_FEj`X'_imp = `r(mean)' if flag_missingFEj`X'_q`Y' == 1
		drop mean_q`Y'_FEj`X'
	}	
}

drop w1 w1_wins wh1 transfer_ny transfer_mtp lnw1_restr lnw1_wins_restr vallazon nap jnap fogvisz1 SLdays FEj ev defl

keep if t==pre_month+1+12
drop t 

merge 1:1 anon pre_month treated matched_vallazon matched_feor1_h2 size_cat controls_we_used n using `tmp'
assert _merge==3
drop _merge
save `tmp', replace


* 12/3) one year before the accident

use `tmp', clear
drop mean* flag*
expand 12
egen t=seq(), by(n)
tab t
replace t=t+pre_month-12

* add quarterly values of wage and firm FE

merge m:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 w1 wh1 transfer_mtp transfer_ny nap jnap fogvisz1 )
rename vallazon1 vallazon
merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", keep(master match) nogen keepusing(vall_fix)
rename vall_fix FEj
merge m:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match) nogen
rename passziv_tip1 SLdays

gen EMP=1 if (vallazon!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (SLdays<6 | SLdays==.) & (fogvisz1==201)

* create a winsorized version of the wage variable, only for the EMP=1
gen w1_wins=w1
egen wp1=pctile(w1) if EMP==1, by(t) p(1)
egen wp99=pctile(w1) if EMP==1, by(t) p(99)
replace w1_wins=wp1 if w1_wins<wp1 & w1_wins!=0 & EMP==1
replace w1_wins=wp99 if w1_wins>wp99 & w1_wins!=. & EMP==1
drop wp1 wp99

cap gen ev=2003+int((t-1)/12)
replace ev=2003+int((t-1)/12)

gen defl=1 if ev==2003
	replace defl = 1.067991144 if ev==2004
	replace defl = 1.106443719 if ev==2005
	replace defl = 1.149586344 if ev==2006
	replace defl = 1.241552086 if ev==2007
	replace defl = 1.317292007 if ev==2008
	replace defl = 1.372611279 if ev==2009
	replace defl = 1.439874155 if ev==2010
	replace defl = 1.496329527 if ev==2011
	replace defl = 1.581158238 if ev==2012
	replace defl = 1.608512002 if ev==2013
	replace defl = 1.604987182 if ev==2014
	replace defl = 1.603851084 if ev==2015
	replace defl = 1.610259846 if ev==2016
	replace defl = 1.648100676 if ev==2017

* cenzoring hours worked
replace wh1=40 if wh1>40 & wh1!=.
replace wh1=20 if wh1<20	
foreach X in "" _wins{
	gen lnw1`X'_restr=log((w1`X'/defl)/((wh1*30)/7)) if transfer_mtp!=401 & transfer_ny!=203
	replace lnw1`X'_restr=. if EMP!=1

}
drop EMP

* create the quarterly averages

foreach X in "" _wins{
	foreach Y of numlist 1/4{
		egen mean_lq`Y'_w`X'_=mean(lnw1`X'_restr) if t<=pre_month-(3*(`Y'-1)) & t>=pre_month-(3*`Y'-1), by(n)
		egen mean_lq`Y'_w`X'=min(mean_lq`Y'_w`X'_), by(n)
		drop mean_lq`Y'_w`X'_
	}
}
foreach X in ""{
	foreach Y of numlist 1/4{
		egen mean_lq`Y'_FEj`X'_=mean(FEj`X') if t<=pre_month-(3*(`Y'-1)) & t>=pre_month-(3*`Y'-1), by(n)
		egen mean_lq`Y'_FEj`X'=min(mean_lq`Y'_FEj`X'_), by(n)
		drop mean_lq`Y'_FEj`X'_
		gen flag_missingFEj`X'_lq`Y' = 1 if mean_lq`Y'_FEj`X' == .
		recode flag_missingFEj`X'_lq`Y' (.=0)
		gen mean_lq`Y'_FEj`X'_imp = mean_lq`Y'_FEj`X'
		sum mean_lq`Y'_FEj`X'
		replace mean_lq`Y'_FEj`X'_imp = `r(mean)' if flag_missingFEj`X'_lq`Y' == 1
		drop mean_lq`Y'_FEj`X'
	}	
}

drop w1 w1_wins wh1 transfer_ny transfer_mtp lnw1_restr lnw1_wins_restr vallazon nap jnap fogvisz1 SLdays FEj ev defl

keep if t==pre_month+1-12
drop t 

merge 1:1 anon pre_month treated matched_vallazon matched_feor1_h2 size_cat controls_we_used n using `tmp'
assert _merge==3
drop _merge
save `tmp', replace

* 12/3) two years before the accident

use `tmp', clear
drop mean* flag*
expand 12
egen t=seq(), by(n)
tab t
replace t=t+pre_month-24

* add quarterly values of wage and firm FE

merge m:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 w1 wh1 transfer_mtp transfer_ny nap jnap fogvisz1 )
rename vallazon1 vallazon
merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", keep(master match) nogen keepusing(vall_fix)
rename vall_fix FEj
merge m:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match) nogen
rename passziv_tip1 SLdays

gen EMP=1 if (vallazon!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (SLdays<6 | SLdays==.) & (fogvisz1==201)

* create a winsorized version of the wage variable, only for the EMP=1
gen w1_wins=w1
egen wp1=pctile(w1) if EMP==1, by(t) p(1)
egen wp99=pctile(w1) if EMP==1, by(t) p(99)
replace w1_wins=wp1 if w1_wins<wp1 & w1_wins!=0 & EMP==1
replace w1_wins=wp99 if w1_wins>wp99 & w1_wins!=. & EMP==1
drop wp1 wp99

cap gen ev=2003+int((t-1)/12)
replace ev=2003+int((t-1)/12)

gen defl=1 if ev==2003
	replace defl = 1.067991144 if ev==2004
	replace defl = 1.106443719 if ev==2005
	replace defl = 1.149586344 if ev==2006
	replace defl = 1.241552086 if ev==2007
	replace defl = 1.317292007 if ev==2008
	replace defl = 1.372611279 if ev==2009
	replace defl = 1.439874155 if ev==2010
	replace defl = 1.496329527 if ev==2011
	replace defl = 1.581158238 if ev==2012
	replace defl = 1.608512002 if ev==2013
	replace defl = 1.604987182 if ev==2014
	replace defl = 1.603851084 if ev==2015
	replace defl = 1.610259846 if ev==2016
	replace defl = 1.648100676 if ev==2017

* cenzoring hours worked
replace wh1=40 if wh1>40 & wh1!=.
replace wh1=20 if wh1<20	
foreach X in "" _wins{
	gen lnw1`X'_restr=log((w1`X'/defl)/((wh1*30)/7)) if transfer_mtp!=401 & transfer_ny!=203
	replace lnw1`X'_restr=. if EMP!=1

}
drop EMP

* create the quarterly averages

foreach X in "" _wins{
	foreach Y of numlist 5/8{
		egen mean_lq`Y'_w`X'_=mean(lnw1`X'_restr) if t<=pre_month-(3*(`Y'-1)) & t>=pre_month-(3*`Y'-1), by(n)
		egen mean_lq`Y'_w`X'=min(mean_lq`Y'_w`X'_), by(n)
		drop mean_lq`Y'_w`X'_
	}
}
foreach X in ""{
	foreach Y of numlist 5/8{
		egen mean_lq`Y'_FEj`X'_=mean(FEj`X') if t<=pre_month-(3*(`Y'-1)) & t>=pre_month-(3*`Y'-1), by(n)
		egen mean_lq`Y'_FEj`X'=min(mean_lq`Y'_FEj`X'_), by(n)
		drop mean_lq`Y'_FEj`X'_
		gen flag_missingFEj`X'_lq`Y' = 1 if mean_lq`Y'_FEj`X' == .
		recode flag_missingFEj`X'_lq`Y' (.=0)
		gen mean_lq`Y'_FEj`X'_imp = mean_lq`Y'_FEj`X'
		sum mean_lq`Y'_FEj`X'
		replace mean_lq`Y'_FEj`X'_imp = `r(mean)' if flag_missingFEj`X'_lq`Y' == 1
		drop mean_lq`Y'_FEj`X'
	}	
}

drop w1 w1_wins wh1 transfer_ny transfer_mtp lnw1_restr lnw1_wins_restr vallazon nap jnap fogvisz1 SLdays FEj ev defl

keep if t==pre_month+1-24
drop t 

merge 1:1 anon pre_month treated matched_vallazon matched_feor1_h2 size_cat controls_we_used n using `tmp'
assert _merge==3
drop _merge

save "$out/coworker_quarter_AB_ModTreated.dta", replace

* adding number of co-workers in firm-occupation and firm-occupation-gender-age group

use "$out/co_workers_with_controls_event_level_20250704_BM.dta", clear
egen numcoworkers=count(anon), by( vallazon1 feor1_h2 pre_month)
* we should add the missing treated to count the total number of co-workers in the treated cells
replace numcoworkers=numcoworkers+1 if treated==1
* we should take out the employee we look at and count only the others
replace numcoworkers=numcoworkers-1
keep anon pre_month treated vallazon1 feor1_h2 numcoworkers size_cat
rename vallazon1 matched_vallazon
rename feor1_h2 matched_feor1_h2
save "$out/num_co_workers_in_premonth_coworker_AB_ModTreated.dta"

use "$out/coworker_quarter_AB_ModTreated.dta", clear
forval i=1/8 {
	local j=`i'+8
	rename mean_q`i'_w w_q`j'
	replace mean_q`i'_FEj_imp =. if flag_missingFEj_q`i'==1
	rename mean_q`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

by anon, sort: gegen mean_treated=mean(treated)
drop if mean_treated==1 //removes those who are never potential control co-workers

merge m:1 anon pre_month using "$out/coworker_random_pre_month.dta", nogen keep(match) //this selects the random pre_month dates we used in the paper

reshape long w_q FE_q, i(anon) j(relative_time)


rename pre_month t 
cap drop date
gen date=t+(relative_time-8)*3

gen ev=floor((t-1)/12)+2003

rename t t_orig
rename date t
merge 1:1 anon t using "$in/admin3_alap.dta", nogen keep(master match) keepusing(ferfi kor vallazon teaor)

rename vallazon1 vallazon
merge m:1 vallazon ev using "$in/admin3_nav_eredeti.dta", nogen keep(master match) keepusing(letszam)
rename t date
rename t_orig t

**firm in 2nd and 3rd months of the quarter
rename vallazon vallazon_m1

rename t t_orig
rename date t
replace t=t+1
merge 1:1 anon t using "$in/admin3_alap.dta", nogen keep(master match) keepusing(ferfi kor vallazon)
replace t=t-1
rename t date
rename t_orig t
rename vallazon1 vallazon_m2

rename t t_orig
rename date t
replace t=t+2
merge 1:1 anon t using "$in/admin3_alap.dta", nogen keep(master match) keepusing(ferfi kor vallazon)
replace t=t-2
rename t date
rename t_orig t
rename vallazon1 vallazon_m3


gen kor_base_=kor if relative_time==0
by anon, sort: egen kor_base=min(kor_base_)
drop kor_base_

gen teaor_base_=teaor if relative_time==0
by anon, sort: egen teaor_base=min(teaor_base)
drop teaor_base_

gen letszam_base_=letszam if relative_time==0
by anon, sort: egen letszam_base=min(letszam_base)
drop letszam_base_

gen letszam_base_2=letszam if relative_time==7
by anon, sort: egen letszam_base2=min(letszam_base_2)
drop letszam_base_2

cap drop rt
gen rt=relative_time
replace rt=7 if treated==0

cap drop after
gen after=0
replace after=1 if rt>=9 & rt<=12
replace after=2 if rt>=13 & rt<=16

cap drop rtdum*
tabulate rt, generate(rtdum)

forval i=2/8 {
	replace rtdum`i'=rtdum`i'-rtdum1
}

winsor2 w_q, cuts(0 99) by(ev)
winsor2 FE_q, cuts(0 99) by(ev)

xtset anon relative_time

cap drop vallazon_base*
gen vallazon_base_=vallazon_m1 if relative_time==7
by anon, sort: egen vallazon_base=min(vallazon_base_)
drop vallazon_base_

gen stay=0
replace stay=1 if vallazon_m1==vallazon_base & vallazon_m2==vallazon_base & vallazon_m3==vallazon_base & vallazon_base~=.
by anon, sort: egen tot_stay=total(stay)

gen control=1-treated
gen korcat=floor(kor_base/5)

***************Incl age & sex in matching*************

cap drop group
*egen group=group(matched_feor1_h2 size_cat korcat ferfi t)
egen group=group(matched_feor1_h2 size_cat kor ferfi t)

cap drop count_treated
by group relative_time, sort: egen count_treated=total(treated) 

cap drop count_control
by group relative_time, sort: egen count_control=total(control) 

cap drop match_sample*
bysort group treated relative_time (anon): gen match_sample_ = _n<=count_treated //in each cell we have at most as many controls as treated
replace match_sample_=. if relative_time~=7
replace match_sample_=. if count_treated>count_control

by anon, sort: egen match_sample=min(match_sample_)
drop match_sample_

***************Incl age, sex and industry in matching*************

cap drop group2
*egen group=group(matched_feor1_h2 size_cat korcat ferfi t)
egen group2=group(matched_feor1_h2 teaor1_h1 size_cat kor ferfi t)

cap drop count_treated2
by group2 relative_time, sort: egen count_treated2=total(treated) 

cap drop count_control2
by group2 relative_time, sort: egen count_control2=total(control) 

cap drop match_sample2*
bysort group2 treated relative_time (anon): gen match_sample2_ = _n<=count_treated2 //in each cell we have at most as many controls as treated
replace match_sample2_=. if relative_time~=7
replace match_sample2_=. if count_treated2>count_control2

by anon, sort: egen match_sample2=min(match_sample2_)
drop match_sample2_


***************Incl age, sex and exact firm size in matching*************

cap drop group3
*egen group=group(matched_feor1_h2 size_cat korcat ferfi t)
egen group3=group(matched_feor1_h2 letszam_base kor ferfi t)

cap drop count_treated3
by group3 relative_time, sort: egen count_treated3=total(treated) 

cap drop count_control3
by group3 relative_time, sort: egen count_control3=total(control) 

cap drop match_sample3*
bysort group3 treated relative_time (anon): gen match_sample3_ = _n<=count_treated3 //in each cell we have at most as many controls as treated
replace match_sample3_=. if relative_time~=7
replace match_sample3_=. if count_treated3>count_control3

by anon, sort: egen match_sample3=min(match_sample3_)
drop match_sample3_

***************Incl age category and sex in matching*************

cap drop group4
*egen group=group(matched_feor1_h2 size_cat korcat ferfi t)
egen group4=group(matched_feor1_h2 size_cat korcat ferfi t)

cap drop count_treated4
by group4 relative_time, sort: egen count_treated4=total(treated) 

cap drop count_control4
by group4 relative_time, sort: egen count_control4=total(control) 

cap drop match_sample4*
bysort group4 treated relative_time (anon): gen match_sample4_ = _n<=count_treated4 //in each cell we have at most as many controls as treated
replace match_sample4_=. if relative_time~=7
replace match_sample4_=. if count_treated4>count_control4

by anon, sort: egen match_sample4=min(match_sample4_)
drop match_sample4_

cap drop count_group1
bys group relative_time t : egen count_group1=count(anon)
replace count_group1=. if relative_time!=0
cap drop tot_group1
by anon, sort: egen tot_group1=min(count_group1)
drop count_group1

cap drop count_group2
bys group2 relative_time t : egen count_group2=count(anon)
replace count_group2=. if relative_time!=0
cap drop tot_group2
by anon, sort: egen tot_group2=min(count_group2)
drop count_group2

cap drop count_group3
bys group3 relative_time t : egen count_group3=count(anon)
replace count_group3=. if relative_time!=0
cap drop tot_group3
by anon, sort: egen tot_group3=min(count_group3)
drop count_group3

cap drop count_group4
bys group4 relative_time t : egen count_group4=count(anon)
replace count_group4=. if relative_time!=0
cap drop tot_group4
by anon, sort: egen tot_group4=min(count_group4)
drop count_group4

bys matched_feor1_h2 kor ferfi relative_time t: egen count_similar_group=count(anon)
replace count_similar_group=. if relative_time!=0
by anon, sort: egen tot_similar_group=min(count_similar_group)
drop count_similar_group

cap drop after
gen after=0
replace after=1 if relative_time>=9 

rename t pre_month
cap drop _merge
merge m:1 anon pre_month matched_vallazon matched_feor1_h2 using "$out/num_co_workers_in_premonth_coworker_AB_ModTreated.dta" //this file is created above in the same code
drop if _merge==2

pwcorr letszam_base numcoworkers if relative_time==0

gen ncoworkers_=numcoworkers if relative_time==0
sort anon pre_month matched_vallazon matched_feor1_h2
by anon pre_month matched_vallazon matched_feor1_h2: egen ncoworkers=min(ncoworkers_)
drop ncoworkers_

save "$out/coworker_regfile_with_ncoworkers_AB_ModTreated_clean.dta", replace
